//  Inside Microsoft Exchange 2007 Web Services 
//  Chapter 4
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.  
//  All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Xml;
using System.IO;
using ProxyHelpers.EWS;

namespace MoveFolderUsingProxy
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeServiceBinding binding = new ExchangeServiceBinding();
            binding.Url = @"https://yourServer/ews/Exchange.asmx";
            binding.Credentials = new NetworkCredential(
                "username",
                "password",
                "domain");

            // We need a folder id in order to call this.  For now, we
            // will just call a helper class to create one for us.
            //

            FolderIdType folderId = EWSHelpers.CreateFolder(
                        binding,
                        "SomeFolder",
                        new DistinguishedFolderIdType(
                            DistinguishedFolderIdNameType.inbox));

            // Now, let's move our folder...
            //
            MoveFolderUsingProxy(binding, folderId);
        }

        /// <summary>
        /// Moves a folder to the sent items using the proxy (Listing 4-8)
        /// </summary>
        /// <param name="binding">Binding to use for the call</param>
        /// <param name="folderToMove">Id of the folder to move</param>
        /// 
        public static void MoveFolderUsingProxy(
                        ExchangeServiceBinding binding,
                        FolderIdType folderToMove)
        {
            MoveFolderType moveRequest = new MoveFolderType();
            moveRequest.ToFolderId = new TargetFolderIdType();

            // Set the parent folder that we want to move our folder to.
            //
            DistinguishedFolderIdType destinationId = new DistinguishedFolderIdType();
            destinationId.Id = DistinguishedFolderIdNameType.sentitems;
            moveRequest.ToFolderId.Item = destinationId;

            // Set the folder to move.  
            //
            moveRequest.FolderIds = new BaseFolderIdType[] { folderToMove };

            // Make the call
            //
            MoveFolderResponseType response = binding.MoveFolder(moveRequest);

            // Since we moved a single item, we can expect a single response message.  
            // The response message will be of type FolderInfoResponseMessageType.  We 
            // must cast it appropriately if we want to get the folder 
            // id that the web method returns.
            //
            FolderInfoResponseMessageType responseMessage =
                    response.ResponseMessages.Items[0] as
                             FolderInfoResponseMessageType;

            if (responseMessage.ResponseCode == ResponseCodeType.NoError)
            {
                Console.WriteLine("Folder moved successfully!  FolderId: " +
                         responseMessage.Folders[0].FolderId.Id);
            }
            else
            {
                Console.WriteLine("Hmmm.  MoveFolder failed.  Error code: " +
                        responseMessage.ResponseCode.ToString());
            }
        }
    }
}
